<template>

  <el-dialog :title="id == null ? '添加' : '编辑'" :visible.sync="dialogVisible" :close-on-click-modal="false">


    <el-form ref="main-form" status-icon :model="mainQuery" :rules="rules" label-width="120px" label-position="left" :inline="false" style="width: 400px;">
      
      <el-form-item label="ID" size="mini" v-if="id != null">
        <el-input placeholder="" onfocus="this.select()" v-model="id" readonly></el-input>
      </el-form-item>

      <el-form-item label="简称" size="mini" prop="countryNameSimple">
        <el-input placeholder="" v-model="mainQuery.countryNameSimple" :disabled="loading" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>
      <el-form-item label="英文" size="mini" prop="countryNameEN">
        <el-input placeholder="" v-model="mainQuery.countryNameEN" :disabled="loading" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>
      <el-form-item label="中文" size="mini" prop="countryNameCN">
        <el-input placeholder="" v-model="mainQuery.countryNameCN" :disabled="loading" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>

      <br>

      <el-form-item label="订单英文" size="mini" prop="ENorder">
        <el-input placeholder="" v-model="mainQuery.ENorder" :disabled="loading" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>
      <el-form-item label="订单中文" size="mini" prop="CNorder">
        <el-input placeholder="" v-model="mainQuery.CNorder" :disabled="loading" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>

      <br>

      <el-form-item label="状态" prop="">
        <el-radio-group v-model="mainQuery.status">
          <el-radio v-for="item in statusOptions" :label="item.value">{{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="创建时间" size="mini" v-if="data != null">
        <el-input placeholder="" v-model="data.createTime" readonly></el-input>
      </el-form-item>
      <el-form-item label="修改时间" size="mini" v-if="data != null">
        <el-input placeholder="" v-model="data.updateTime" readonly></el-input>
      </el-form-item>

    </el-form>

      <div slot="footer" class="dialog-footer">

        <el-popover
          v-if="id != null"
          placement="top"
          width="160"
          v-model="popDeleteVisible">
          <p>确定删除吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="default" @click="popDeleteVisible = false">取消</el-button>
            <el-button type="danger" size="mini" @click="submitDel">确定</el-button>
          </div>
          <el-button type="danger" slot="reference"  size="small" style="float: left;" :loading="loading">删 除</el-button>
        </el-popover>

        <el-button @click="close" size="mini">取 消</el-button>
        <el-button @click="submit" size="mini" type="primary" :loading="loading">提 交</el-button>
      </div>
  </el-dialog>
</template>

<script>

import ApiMain from '@/api/platform.country';

export default {
  mixins: [
  ],
  props: [
    'statusOptions',
  ],
  data () {
    return {

      dialogVisible   : false,
      popDeleteVisible: false,
      loading         : false,

      mainQueryRaw: null,
      mainQuery: {
        countryNameSimple: '',
        countryNameEN: '',
        countryNameCN: '',
        ENorder: '',
        CNorder: '',
        status: 0,
      },

      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' },
        ],
      },

      id  : null,
      data: null,

    }
  },
  mounted () {
    this.mainQueryRaw = Object.copy(this.mainQuery);
  },

  methods: {

    show (id) {

      this.reset();

      if (id) {
        this.id = id;
        this.fetchData();
      }

      this.dialogVisible = true;
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.mainQuery = Object.copy(this.mainQueryRaw);
      this.$refs['main-form'] && this.$refs['main-form'].resetFields();
        
      this.id   = null;
      this.data = null;
    },

    async fetchData () {

      try {
        var re = await ApiMain.info({id: this.id});
        this.data = re;
      } catch (err) {
        this.data = null;
      }

      if (this.data) {
        this.mainQuery.countryNameSimple   = this.data.countryNameSimple;
        this.mainQuery.countryNameEN   = this.data.countryNameEN;
        this.mainQuery.countryNameCN   = this.data.countryNameCN;
        this.mainQuery.ENorder   = this.data.ENorder;
        this.mainQuery.CNorder   = this.data.CNorder;
        this.mainQuery.status = this.data.status;
      }

    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      if (this.id) {
        await this.submitSet();
      }
      else {
        await this.submitAdd();
      }

      this.loading = false;

    },

    async submitAdd () {

      try {
        await ApiMain.add(this.mainQuery);
        this.$message({message: '添加成功', type: 'success'});
        this.close();
      } catch (err) {
      }

    },

    async submitSet () {

      this.mainQuery.id = this.id;

      try {
        await ApiMain.set(this.mainQuery);
        this.$message({message: '修改成功', type: 'success'});
        this.close();
      } catch(e) {
      }

    },

    async submitDel () {
      this.popDeleteVisible = false;
      this.loading = true;

      try {
        await ApiMain.del({id: this.id})
        this.$message({message: '删除成功', type: 'success'});
        this.close();
      } catch(e) {
      }

      this.loading = false;

    },
  }
}
</script>

<style>

</style>
